package com.example.yqand4v1_1.service;

import com.example.yqand4v1_1.dao.entity.News;
import com.example.yqand4v1_1.dao.NewsCollectMapper;
import com.example.yqand4v1_1.dto.NewsCollectDTO;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class NewsCollectService {

    @Resource
    private NewsCollectMapper newsCollectMapper;

    @Resource
    private NewsService newsService;

    public void collect(NewsCollectDTO newsCollectDTO) {
        newsCollectMapper.addVolunteerCollect(newsCollectDTO.getUserId(), newsCollectDTO.getNewsId());
    }

    // 显示收藏列表
    public List<News> list(int userId) {
        List<News> ret = new ArrayList<>();
        List<Integer> ids = newsCollectMapper.getVolunteerCollection(userId); // 获得该志愿者收藏news的ids
        if (CollectionUtils.isEmpty(ids)) {
            return ret; // 返回空表
        }
        ret.addAll(newsService.getByIds(ids));
        return ret;
    }

    public void removeCollect(int userId, int newsId) {
        newsCollectMapper.removeNewsCollect(userId, newsId);
    }
}
